Element.prototype.dragNclick = (function (menu, elemClick) {

    var bTime = 0,
        eTime = 0,
        oPos = [],
        cbTime = 0, //记录鼠标按下的时间戳
        ceTime = 0, //记录鼠标抬起的时间戳
        counter = 0,
        t = null,
        wWidth = getViewPortSize().width, // 浏览器窗口的宽度
        wHeight = getViewPortSize().height,
        elemWidth = parseInt(getStyles(this, 'width')), //a标签的宽度
        elemHeight = parseInt(getStyles(this, 'height')),
        mWidth = parseInt(getStyles(this, 'width')), 
        mHeight = parseInt(getStyles(this, 'height'));

    function drag() {
        var x,
            y,
            _self = this;

        addEvent(this, 'mousedown', function (e) {
            var e = e || window.event,
                btnCode = e.button;
            // 判断如果是点击鼠标的右键 那么就打开菜单
            if (btnCode === 2) {
                var mLeft = pagePos(e).X,
                    mTop = pagePos(e).Y;
                // 菜单同样要做边界判断
                if (mLeft <= 0) {
                    mLeft = 0;
                } else if (mLeft >= wWidth - mWidth) {
                    // 在最右边的时候 让它显示在左边
                    mLeft = pagePos(e).X - mWidth;
                }

                if (mTop <= 0) {
                    mTop = 0;
                } else if (mTop >= wHeight - mHeight) {
                    // 在最下面的时候 显示在右上角
                    mTop = pagePos(e).Y - mHeight;
                }

                menu.style.left = mLeft + 'px';
                menu.style.top = mTop + 'px';
                menu.style.display = 'block';
            } else if (btnCode === 0) {
                bTime = new Date().getTime();
                oPos = [parseInt(getStyles(_self, 'left')), parseInt(getStyles(_self, 'top'))];
                menu.style.display = 'none';
                x = pagePos(e).X - parseInt(getStyles(_self, 'left'));
                y = pagePos(e).Y - parseInt(getStyles(_self, 'top'));

                addEvent(document, 'mousemove', mouseMove);
                addEvent(document, 'mouseup', mouseUp);
                cancelBubble(e);
                preventDefaultEvent(e);
            }
        });

        addEvent(document, 'contextmenu', function (e) {
            var e = e || window.event;
            preventDefaultEvent(e);
        });

        addEvent(document, 'click', function (e) {
            menu.style.display = 'none';
        });

        addEvent(menu, 'click', function (e) {
            var e = e || window.event;
            cancelBubble(e);
        });

        function mouseMove(e) {
            var e = e || window.event,
                elemLeft = pagePos(e).X - x,
                elemTop = pagePos(e).Y - y;
            // 判断拖拽的边界
            if (elemLeft <= 0) {
                elemLeft = 0;
            } else if (elemLeft >= wWidth - elemWidth) {
                elemLeft = wWidth - elemWidth - 1;
            }

            if (elemTop <= 0) {
                elemTop = 0;
            } else if (elemTop >= wHeight - elemHeight) {
                elemTop = wHeight - elemHeight - 1;
            }


            _self.style.left = elemLeft + 'px';
            _self.style.top = elemTop + 'px';
        }

        function mouseUp(e) {
            var e = e || window.event;
            eTime = new Date().getTime();

            // 模拟点击事件
            if (eTime - bTime < 100) {
                _self.style.left = oPos[0] + 'px';
                _self.style.top = oPos[1] + 'px';
                // 这里来给counter的累加来判断点击的次数
                counter++;
                if (counter === 1) {
                    cbTime = new Date().getTime();
                }

                if (counter === 2) {
                    ceTime = new Date().getTime();
                }
                // 两次点击时间戳的差值来判断 是否是双击事件
                if (cbTime && ceTime && (ceTime - cbTime < 200)) {
                    elemClick();
                }
                // 给一个定时器来还原初始的时间戳与点击次数
                t = setTimeout(function () {
                    cbTime = 0;
                    ceTime = 0;
                    counter = 0;
                    clearTimeout(t);
                }, 200)
            }
            removeEvent(document, 'mousemove', mouseMove);
            removeEvent(document, 'mouseup', mouseUp);
        }
    }
    drag.call(this);
});